package com.feiyu.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyu.common.cache.CacheNames;
import com.feiyu.common.constant.Constant;
import com.feiyu.common.constant.ResultCode;
import com.feiyu.common.exception.GlobalException;
import com.feiyu.user.dto.UpdateInfo;
import com.feiyu.user.entity.User;
import com.feiyu.user.entity.UserInfo;
import com.feiyu.user.mapper.UserMapper;
import com.feiyu.user.service.UserService;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.RedisUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tang ming
 * @since 2021-11-30
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private PasswordEncoder passwordEncoder;

    private RedisUtil redisUtil;


    /**
    * 登场是，获取用户信息 用来验证
    * */
    @Override
    public UserInfo userInfo(String account) {
        return baseMapper.getUserInfoByAccount(account);
    }

    /**
    * 保存新账号
    * */
    @Override
    public boolean saveNewAccount(User user) {
        //1.加密密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //2.设置默认登录名为用户名
        user.setAccount(user.getName());
        //3.设置默认头像
        user.setAvatar(Constant.DEFAULT_AVATAR);
        //4.保存到数据库
        return save(user);
    }

    @Override
    public boolean existAccount(String account) {
        return baseMapper.existAccount(account)>0;
    }
    @Override
    public boolean existEmail(String email) {
        return baseMapper.existEmail(email)>0;
    }
    /**
    * 注册用户
    * */
    @Override
    public boolean signUp(User user) {
        //先判断账号是否存在
        if (existAccount(user.getName()))
            throw new GlobalException(ResultCode.USER_EXIST);
        if (existEmail(user.getEmail()))
            throw new GlobalException(ResultCode.EMAIL_EXIST);
        HttpServletRequest request = WebUtil.getRequest();
        String emailCode = request.getHeader(Constant.EMAIL_CODE);
        String redisCode=String.valueOf(redisUtil.get(CacheNames.EMAIL_CODE+user.getEmail()));
        if (emailCode==null||!emailCode.equals(redisCode))
            throw new GlobalException(ResultCode.CODE_ERROR);
        return saveNewAccount(user);
    }

    @Override
    public Boolean updatePassword(UpdateInfo user) {
        if (passwordEncoder.matches(user.getOldPassword(),user.getPassword())) {
            user.setPassword(passwordEncoder.encode(user.getNewPassword()));
            return updateById(user);
        }
        return false;
    }

    @Override
    public Boolean updateEmail(UpdateInfo user) {
        if (existEmail(user.getNewEmail()))
            return false;
        String code=(String)redisUtil.get(CacheNames.EMAIL_CODE+user.getEmail());
        if (user.getVerifyCode()!=null&&code!=null&&code.equals(user.getVerifyCode())){
            User update = new User();
            update.setId(user.getId());
            update.setEmail(user.getNewEmail());
           return updateById(update);
        }
        return false;
    }
}
